from com.kjssoft.eagleye.models import EEUser
from com.kjssoft.rainbow import LOGGER
from com.kjssoft.rainbow.album.models import RainbowPhotoFrameAlbum
from com.kjssoft.rainbow.consts import PhotoFrameConsts
from com.kjssoft.rainbow.frame.models import RainbowPhotoFrame

__author__ = 'Sea'

class UserAlbumProfile(object):
    def __init__(self, user, album = None):
        super(UserAlbumProfile, self).__init__()
        self.user = user
        self.album = album
        self.is_owner = False
        self.is_auth = False
        self.is_auth_owner = False
        if self.user and isinstance(self.user, EEUser):
            if self.user.is_authenticated():
                self.is_auth = True
            if self.album and isinstance(self.album, RainbowPhotoFrameAlbum):
                if self.user == self.album.user:
                    self.is_owner = True
        if self.is_owner and self.is_auth:
            self.is_auth_owner = True

    def _get_albums(self):
        albums = []
        if self.user and isinstance(self.user, EEUser):
            albums =  RainbowPhotoFrameAlbum.objects.filter(
                user = self.user
            ).order_by('create_ms')
        return albums
    albums = property(_get_albums)

    def _get_album_photos(self):
        LOGGER.debug("rainbow album user %s is logged? %s" % (self.user, self.user.is_authenticated()))
        if self.is_auth_owner:
            photo_scope = [PhotoFrameConsts.Scope.SELF, PhotoFrameConsts.Scope.FRIEND, PhotoFrameConsts.Scope.PUBLIC]
            photo_status = [PhotoFrameConsts.Status.DRAFT, PhotoFrameConsts.Status.SUBMIT, PhotoFrameConsts.Status.VERIFYING, PhotoFrameConsts.Status.VERIFIED]
        else:
            photo_scope = [PhotoFrameConsts.Scope.PUBLIC]
            photo_status = [PhotoFrameConsts.Status.VERIFIED]
        return RainbowPhotoFrame.objects.filter(
            album      = self.album,
            scope__in  = photo_scope,
            status__in = photo_status
        )
    album_photos = property(_get_album_photos)

    def _get_snapshot_photos(self):
        album_photos = self._get_album_photos()
        LOGGER.debug("album photos %s" % album_photos)
        return album_photos[0:9]
    snapshot_photos = property(_get_snapshot_photos)